<?php/*  PHP version 5  Copyright (c) 2002-2019ECISP.CN、EarcLink.COM  警告：这不是一个免费的软件，请在许可范围内使用，请尊重知识产权，侵权必究，举报有奖  作者：黄祥云 E-mail:6326420@qq.com  QQ:6326420 TEL:18665655030 / 13797552220*/


namespace application;

class ESPCMS_Change {

	public static function has_category($in_tid, $array, $keyname = 'tid', $lastkeyname = 'upid') {
		if (!is_array($array) || count($array) <= 0) {
			return false;
		}

		$level = 0;

		$last_tid = 0;

		$options = array();

		$tid_array = array();

		$level_array = array();
		while (!empty($array)) {
			foreach ($array AS $key => $value) {
				$tid = $value[$keyname];

				if ($level == 0 && $last_tid == 0) {
					$options[$tid] = $value;
					$options[$tid]['level'] = $level;
					$options[$tid]['id'] = $tid;

					$idcode = $tid;
					$options[$tid]['pathid'] = $tid;
					$options[$tid]['parentpath'] = $value[$lastkeyname];

					unset($array[$key]);

					if ($value['has_c'] == 0) {
						continue;
					}

					$last_tid = $tid;

					$tid_array = array($tid);

					$level_array[$last_tid] = ++$level;

					continue;
				}

				if ($value[$lastkeyname] == $last_tid) {
					$options[$tid] = $value;
					$options[$tid]['level'] = $level;
					$options[$tid]['id'] = $tid;
					$department_lastid = $value[$lastkeyname];


					$options[$tid]['pathid'] = $options[$department_lastid]['pathid'] . '-' . $value[$keyname];
					$options[$tid]['parentpath'] = $options[$department_lastid]['pathid'];
					unset($array[$key]);

					if ($value['has_c'] > 0) {

						if (end($tid_array) != $last_tid) {
							$tid_array[] = $last_tid;
						}

						$last_tid = $tid;

						$tid_array[] = $tid;

						$level_array[$last_tid] = ++$level;
					}
				} elseif ($value[$lastkeyname] > $last_tid) {
					break;
				}
			}

			$count = count($tid_array);
			if ($count > 1) {

				$last_tid = array_pop($tid_array);
			} elseif ($count == 1) {


				if ($last_tid != end($tid_array)) {
					$last_tid = end($tid_array);
				} else {

					$level = 0;
					$last_tid = 0;
					$tid_array = array();
					continue;
				}
			}
			if ($last_tid && isset($level_array[$last_tid])) {
				$level = $level_array[$last_tid];
			} else {
				$level = 0;
			}
		}

		if (!$in_tid) {

			$options_array = array();
			foreach ($options as $key => $value) {
				$options_array[] = $value;
			}
			return $options_array;
		} else {


			if (empty($options[$in_tid])) {
				return array();
			}

			$in_tid_level = $options[$in_tid]['level'];

			foreach ($options AS $key => $value) {
				if ($key != $in_tid) {
					unset($options[$key]);
				} else {
					break;
				}
			}
			$in_tid_array = array();
			foreach ($options AS $key => $value) {
				if (($in_tid_level == $value['level'] && $value[$keyname] != $in_tid) ||
					($in_tid_level > $value['level'])) {
					break;
				} else {
					$in_tid_array[$key] = $value;
				}
			}
			return $in_tid_array;
		}
	}

	public static function getTree($typearray, $tid = 'tid', $upid = 'upid', $child = 'childArray', $root = 0) {
		$treeArray = array();
		if (is_array($typearray)) {
			$array = array();
			foreach ($typearray as $key => $item) {
				$array[$item[$tid]] = & $typearray[$key];
			}
			foreach ($typearray as $key => $item) {
				$parentId = $item[$upid];
				if ($root == $parentId) {
					$treeArray[] = &$typearray[$key];
				} else {
					if (isset($array[$parentId])) {
						$parent = &$array[$parentId];
						$parent[$child][] = &$typearray[$key];
					}
				}
			}
		}
		return $treeArray;
	}

}
